package com.alex.goods.controller;

import com.alex.goods.entity.Goods;
import com.alex.goods.service.GoodsService;
import com.alex.goods.service.impl.GoodsServiceImpl;
import com.alex.goods.util.Blank;
import com.alex.goods.vo.LayuiTableVo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/goods")
public class GoodsServlet extends BasicServlet {
	private static final long serialVersionUID = 1L;
	GoodsService service = new GoodsServiceImpl();
	public Object selectGoodsList(Integer page, Integer limit,String goodsName,
			String standard1, String standard2, String standard3, 
			String material1 ,String material2, String material3,String material4 ,
			String isin ,  String isout, 
			String num ,
			String notInOut,
			HttpServletResponse response) {

		String sqlfrom = "  select * from Goods  ";

		sqlfrom +=   "  where  isuse = 'on'   " ;

		String sqlto  =		 "   limit ?,?  ";


		if(!Blank.isBlank(goodsName)||!Blank.isBlank(standard1)||!Blank.isBlank(standard2)||
		  !Blank.isBlank(standard3)||! Blank.isBlank(material1)||!Blank.isBlank(material2)
				||!Blank.isBlank(material3)||!Blank.isBlank(material4)||!Blank.isBlank(isin)
				||!Blank.isBlank(isout) || !Blank.isBlank(num)||!Blank.isBlank(notInOut)
				) {
			 sqlfrom+= "   and   ";
		}
		
		int flag = 0;

		if(!Blank.isBlank(goodsName)) {
			sqlfrom = sqlfrom + " instr(goods_name,  '" + goodsName + "'  ) >0  ";		
		    flag = 1;
		}
		
		
		
		if(!Blank.isBlank(standard1) || !Blank.isBlank(standard2) ||!Blank.isBlank(standard3) ) {	
			int tmpflag = 0;
			
			if(flag==1) 
				sqlfrom += "  and  ";
			
			sqlfrom+="  ( ";
			
			if(!Blank.isBlank(standard1)) {
				sqlfrom += "     standard =  '" + standard1+ "'   "; 
				tmpflag = 1;
			}
			
			if(!Blank.isBlank(standard2)) {
				if( tmpflag==1 ) {
					sqlfrom += " or  ";
				}
				sqlfrom += "   standard =  '" + standard2+ "'   "; 
				tmpflag = 1;
			}
			if(!Blank.isBlank(standard3)) {
				if(tmpflag == 1) sqlfrom += "  or ";
				sqlfrom += "    standard =  '" + standard3+ "'    "; 
				tmpflag = 1;
			}
			sqlfrom += "   )  ";
			flag = 1;
		}
		
		if(!Blank.isBlank(material1) || !Blank.isBlank(material2)|| !Blank.isBlank(material3)
			|| !Blank.isBlank(material4)			
				) {
			
			if(flag == 1)
				sqlfrom += "  and  "; 
			
			sqlfrom+="  ( ";
			
			int tmpflag = 0;
			
			if(!Blank.isBlank(material1)) {
				
			   	sqlfrom+= "   material = '"+ material1 + "'  ";
			    tmpflag = 1;
			}
			
			if(!Blank.isBlank(material2)) {
				if(tmpflag == 1) {
					sqlfrom+= "   or  ";
				}
			   	sqlfrom+= "   material = '"+material2+ "'  ";
			   	tmpflag = 1;
			}
		
			if(!Blank.isBlank(material3)) {
				
				if(tmpflag==1) {
					sqlfrom+= "   or  ";
				}
				
			   	sqlfrom+= "   material = '"+material3+ "'  ";
			   	tmpflag = 1;
			   	
			}
			
			if(!Blank.isBlank(material4)) {
				
				if(tmpflag == 1) sqlfrom += "  or ";
				
			   	sqlfrom+= "   material = '"+material4+ "'  ";
			   	tmpflag = 1;
			   	
			}
			sqlfrom += "   )  ";
			flag = 1;		
		}
		
	
	
		if(Blank.isBlank(notInOut)){		
			if(!Blank.isBlank(isin)  || !Blank.isBlank(isout)) {
				int tmpflag = 0;	
				if(flag == 1) {
					sqlfrom+="  and  " ;
				}
				sqlfrom+="  ( ";
	            if(!Blank.isBlank(isin)) {
	            	sqlfrom += "   isin = '" + isin + "' ";
	            	tmpflag = 1;
	            }
	            
	            if(!Blank.isBlank(isout)) {
	            	if(flag==1) sqlfrom+= " or ";
	            	sqlfrom += "   isout = '" + isout + "' ";
	            	tmpflag = 1;
	            }
				flag = 1;
				sqlfrom += "   )  ";
			}			
		} else {
				if(flag == 1) {
					sqlfrom+="   and  " ;
				}
				sqlfrom+="  ( ";
				
				sqlfrom += " isin='off' and isout='off'    ";
				
				flag = 1;
				
				sqlfrom += "   )  ";	
		}
	

		if(!Blank.isBlank(num)) {
			
			if(flag == 1) {
				sqlfrom += "  and ";
			}
			sqlfrom += " ( ";
			sqlfrom += "    num =   '" + Integer.valueOf(num) + "'   ";
			sqlfrom+="  ) ";
			flag = 1;
		}

		String sqlcount = "select count(1) from  (  " + sqlfrom +  "   ) as z";
		
		System.out.println(sqlcount);
		
		String sql = sqlfrom+sqlto;

		System.out.println(sql);
		System.out.println(page);
	
		List<Goods> list = service.selectGoodsList(sql, (page - 1) * (limit), limit);
		LayuiTableVo layuiTableVo = new LayuiTableVo();
		layuiTableVo.setCode(0);

		System.out.println(list);

		layuiTableVo.setCount(service.getGoodsKindNumUpdate(sqlcount));
		layuiTableVo.setData(list);
		System.out.println(list);
        System.out.println("ok");
		return layuiTableVo;
	}
	

	public void addGoods(String goodsName, String standard, String material, String isin, String isout) {
		
		if (Blank.isBlank(isin)) {
			isin = "off";
		}
		if (Blank.isBlank(isout)) {
			isout = "off";
		}
		service.addGoods(service.makeGoodsNo(), goodsName, standard, material, isin, isout);
	}
	
	public void updateGoodsInOut(String id,String isin,String isout) {
		if (Blank.isBlank(isin)) {
			isin = "off";
		}
		if (Blank.isBlank(isout)) {
			isout = "off";
		}
		service.updateGoodsInOut( id,isin,isout);
	}


	public Object selectGoodsListNotUse(Integer page, Integer limit,String goodsName,
								  String standard1, String standard2, String standard3,
								  String material1 ,String material2, String material3,String material4 ,
								  String isin ,  String isout,
								  String num ,
								  String notInOut,
								  HttpServletResponse response) {

		String sqlfrom = "  select * from Goods  ";

		sqlfrom +=   "  where  isuse = 'off'   " ;

		String sqlto  =		 "   limit ?,?  ";


		if(!Blank.isBlank(goodsName)||!Blank.isBlank(standard1)||!Blank.isBlank(standard2)||
				!Blank.isBlank(standard3)||! Blank.isBlank(material1)||!Blank.isBlank(material2)
				||!Blank.isBlank(material3)||!Blank.isBlank(material4)||!Blank.isBlank(isin)
				||!Blank.isBlank(isout) || !Blank.isBlank(num)||!Blank.isBlank(notInOut)
		) {
			sqlfrom+= "   and   ";
		}

		int flag = 0;

		if(!Blank.isBlank(goodsName)) {
			sqlfrom = sqlfrom + " instr(goods_name,  '" + goodsName + "'  ) >0  ";
			flag = 1;
		}



		if(!Blank.isBlank(standard1) || !Blank.isBlank(standard2) ||!Blank.isBlank(standard3) ) {
			int tmpflag = 0;

			if(flag==1)
				sqlfrom += "  and  ";

			sqlfrom+="  ( ";

			if(!Blank.isBlank(standard1)) {
				sqlfrom += "     standard =  '" + standard1+ "'   ";
				tmpflag = 1;
			}

			if(!Blank.isBlank(standard2)) {
				if( tmpflag==1 ) {
					sqlfrom += " or  ";
				}
				sqlfrom += "   standard =  '" + standard2+ "'   ";
				tmpflag = 1;
			}
			if(!Blank.isBlank(standard3)) {
				if(tmpflag == 1) sqlfrom += "  or ";
				sqlfrom += "    standard =  '" + standard3+ "'    ";
				tmpflag = 1;
			}
			sqlfrom += "   )  ";
			flag = 1;
		}

		if(!Blank.isBlank(material1) || !Blank.isBlank(material2)|| !Blank.isBlank(material3)
				|| !Blank.isBlank(material4)
		) {

			if(flag == 1)
				sqlfrom += "  and  ";

			sqlfrom+="  ( ";

			int tmpflag = 0;

			if(!Blank.isBlank(material1)) {

				sqlfrom+= "   material = '"+ material1 + "'  ";
				tmpflag = 1;
			}

			if(!Blank.isBlank(material2)) {
				if(tmpflag == 1) {
					sqlfrom+= "   or  ";
				}
				sqlfrom+= "   material = '"+material2+ "'  ";
				tmpflag = 1;
			}

			if(!Blank.isBlank(material3)) {

				if(tmpflag==1) {
					sqlfrom+= "   or  ";
				}

				sqlfrom+= "   material = '"+material3+ "'  ";
				tmpflag = 1;

			}

			if(!Blank.isBlank(material4)) {

				if(tmpflag == 1) sqlfrom += "  or ";

				sqlfrom+= "   material = '"+material4+ "'  ";
				tmpflag = 1;

			}
			sqlfrom += "   )  ";
			flag = 1;
		}



		if(Blank.isBlank(notInOut)){
			if(!Blank.isBlank(isin)  || !Blank.isBlank(isout)) {
				int tmpflag = 0;
				if(flag == 1) {
					sqlfrom+="  and  " ;
				}
				sqlfrom+="  ( ";
				if(!Blank.isBlank(isin)) {
					sqlfrom += "   isin = '" + isin + "' ";
					tmpflag = 1;
				}

				if(!Blank.isBlank(isout)) {
					if(flag==1) sqlfrom+= " or ";
					sqlfrom += "   isout = '" + isout + "' ";
					tmpflag = 1;
				}
				flag = 1;
				sqlfrom += "   )  ";
			}
		} else {
			if(flag == 1) {
				sqlfrom+="   and  " ;
			}
			sqlfrom+="  ( ";

			sqlfrom += " isin='off' and isout='off'    ";

			flag = 1;

			sqlfrom += "   )  ";
		}


		if(!Blank.isBlank(num)) {

			if(flag == 1) {
				sqlfrom += "  and ";
			}
			sqlfrom += " ( ";
			sqlfrom += "    num =   '" + Integer.valueOf(num) + "'   ";
			sqlfrom+="  ) ";
			flag = 1;
		}

		String sqlcount = "select count(1) from  (  " + sqlfrom +  "   ) as z";

		System.out.println(sqlcount);

		String sql = sqlfrom+sqlto;

		System.out.println(sql);
		System.out.println(page);

		List<Goods> list = service.selectGoodsList(sql, (page - 1) * (limit), limit);
		LayuiTableVo layuiTableVo = new LayuiTableVo();
		layuiTableVo.setCode(0);

		System.out.println(list);

		layuiTableVo.setCount(service.getGoodsKindNumUpdate(sqlcount));
		layuiTableVo.setData(list);
		System.out.println(list);
		System.out.println("ok");
		return layuiTableVo;
	}
	public void updateGoodsToNotUse(String goodsNo){
		service.updateGoodsToNotUse(goodsNo);
	}

	public void updateGoodsToUse(String goodsNo){
		service.updateGoodsToUse(goodsNo);
	}

}
